package xianchengpool;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;


import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

@Slf4j
public class Fixd {
    public static Object u = new Object();
    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    static ChangeObjectThread t2 = new ChangeObjectThread("t2");

    public static class ChangeObjectThread extends Thread {
        public ChangeObjectThread(String name) {
            super(name);
        }
        @SneakyThrows
        @Override
        public void run() {
            synchronized (u) {
                System.out.println("in " + getName());
                LockSupport.park();
 //               Thread.sleep(100);
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(getName()+"被中断了");
                }
                System.out.println(getName()+"继续执行");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        t1.start();
        Thread.sleep(1000L);
        t2.start();
        Thread.sleep(3000L);
        t1.interrupt();
        LockSupport.unpark(t2);
        t1.join();
        t2.join();
    }
    @Test
    public void usesyncQueue() throws InterruptedException{
        ExecutorService service =  Executors.newFixedThreadPool(2);
        SynchronousQueue<Object> synchronousQueue = new SynchronousQueue<>();
        Runnable producer=()->{
            Object o = new Object();
            try {
                synchronousQueue.put(o);
            } catch (InterruptedException e) {
                log.info(e.getMessage());
            }
            log.info("product"+o);
        };
        Runnable consumer=()->{

            try {
                Object o = synchronousQueue.take();
                log.info("consumer"+o);
            } catch (InterruptedException e) {
                log.info(e.getMessage());
            }
        };
        service.submit(producer);
        service.submit(consumer);

        service.awaitTermination(5000, TimeUnit.MILLISECONDS);
        service.shutdown();
    }
}
